//
//  DLMachineBaseCell.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/28.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLMachineBaseCell.h"
#import "DLMachineModel.h"

@implementation DLMachineBaseCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // Initialization code
}


+ (CGFloat) getCellHeightWithModel:(DLMachineModel *)model {
    
    /**
        SocketMsgType_text = 10,//普通文字
        SocketMsgType_video,//视频
        SocketMsgType_image,//图片
        SocketMsgType_kLineChart,//k线图
        SocketMsgType_pieChart,//饼图
        SocketMsgType_histogram,//柱状图
        SocketMsgType_lineChart,//折线图(分时图)
        SocketMsgType_form,//图表
        SocketMsgType_information//资讯
    */
    
    CGFloat cellHeight = 0.0;
    
    switch (model.msgType) {
        case SocketMsgType_text:
        {
            
            //因为加了富文本，后面加了一张图片，所以计算文字宽度的时候 后面拼接了一个“一”字，补多出来的图片占的1个长度
            NSString *newContent = @"";
            
            if (model.isServerMsgCanTransformToVoice == YES) {
                newContent = [NSString stringWithFormat:@"%@一nn", model.content];
            } else {
                newContent = model.content;
            }
            
            NSString *contentWithoutHuanHang1 = [newContent stringByReplacingOccurrencesOfString:@"\r" withString:@""];
            NSString *contentWithoutHuanHang = [contentWithoutHuanHang1 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            
            CGFloat newWidth = 0.00;
            
            CGFloat newHeight = 0.00;
            
            if (model.msgFrom == SocketMsgFrom_self) {
                
                newWidth = [contentWithoutHuanHang getValue4WidWithHeight:kMachineNormalTCLabelHeight andFont:kMachineNormalTCTextFontSelf];
                
                newHeight = [newContent getValue4HeightWithWid:kMachineNormalTCLabelMaxWidth andFont:kMachineNormalTCTextFontSelf];
            } else {
                
                newWidth = [contentWithoutHuanHang getValue4WidWithHeight:kMachineNormalTCLabelHeight andFont:kMachineNormalTCTextFontOther];
                
                newHeight = [newContent getValue4HeightWithWid:kMachineNormalTCLabelMaxWidth andFont:kMachineNormalTCTextFontOther];
            }
            
            if (newWidth + 15 < kMachineNormalTCLabelMaxWidth) {
                //
                
                if (newHeight <= kMachineNormalTCLabelHeight) {
                    newHeight = kMachineNormalTCLabelHeight;
                    //
                    cellHeight = kMachineNormalTCLabelHeight + kMachineNormalTCLabelEdgeInsetTopBottom * 2;//(kMachineNormalTCLabelHeight + kMachineNormalTCLabelEdgeInsetLeftRight)
                } else {
                    //
                    cellHeight = newHeight + kMachineNormalTCLabelEdgeInsetTopBottom * 4;//(kMachineNormalTCLabelHeight + kMachineNormalTCLabelEdgeInsetLeftRight)
                }
                
            } else {
                //
                cellHeight = newHeight + kMachineNormalTCLabelEdgeInsetTopBottom * 4;
            }
            
            if (model.isDefaultMachineMsg == YES) {
//                NSLog(@"DLMachineBaseCell:是第一条默认消息");
                cellHeight = [model.content getValue4HeightWithWid:kMachineNormalTCLabelMaxWidth andFont:kBoldFont(18)] + kMachineNormalTCLabelEdgeInsetTopBottom * 2;
            }
        }
            break;
        case SocketMsgType_video:
        {
            
            cellHeight = kMachineVideoViewDefaultHeight;
        }
            break;
        case SocketMsgType_image:
        {
            
            cellHeight = kMachineImageViewDefaultHeight + 30;
        }
            break;
        case SocketMsgType_kLineChart:
        {
            
            cellHeight = kMachineChartsHeight;
        }
            break;
        case SocketMsgType_pieChart:
        {
            
            cellHeight = kMachineChartsHeight;
        }
            break;
        case SocketMsgType_histogram:
        {
            
            cellHeight = kMachineChartsHeight;
        }
            break;
        case SocketMsgType_timeSharing:
        {
            
            cellHeight = kMachineChartsHeight;
        }
            break;
        case SocketMsgType_form:
        {
            //这里判断如果图表只有8条以下的数据，就全部展示 不需要展开收起功能 同时隐藏展开收起按钮
            NSDictionary *dic = model.resultDic[@"data"];
            NSArray *firstColArr = dic[@"kData"];
            NSString *rowNumStr = dic[@"nRow"];
            
            if (firstColArr.count > 0 && firstColArr.count < 9) {
                
                cellHeight = rowNumStr.integerValue *kMachineFormTableViewRowHeight + kMachineFormTitleLabelHeight * 2 + kMachineFormControlEdgeInset *2 + 40;
            } else {
                if (model.isFormShowAllData == YES) {
                    
                    cellHeight = rowNumStr.integerValue *kMachineFormTableViewRowHeight + kMachineFormTitleLabelHeight * 2 + kMachineFormBottomBtnHeight + kMachineFormControlEdgeInset *2 + 40;
                } else {
                    cellHeight = kMachineFormBgViewDefaultHeight + kMachineFormControlEdgeInset *2;
                }
            }

        }
            break;
        case SocketMsgType_information:
        {
            NSArray *arr = model.resultDic[@"data"];
            
            if (arr.count > 0 && arr.count < 9) {
                
                cellHeight = arr.count *kMachineInfoTitleLabelHeight + kMachineInfoBgViewTopMargin *2;
            } else {
                
                if (model.isInfoShowAllData == YES) {
                    
                    cellHeight = arr.count *kMachineInfoTitleLabelHeight + kMachineInfoBottomBtnHeight + kMachineInfoBgViewTopMargin *2 + 20;
                } else {
                    
                    cellHeight = 5 *kMachineInfoTitleLabelHeight + kMachineInfoBottomBtnHeight + kMachineInfoBgViewTopMargin *2 + 20;
                }
            }
            
        }
            break;
        case SocketMsgType_singleSelect:
        {
            
            cellHeight = 30 * model.resultArr.count + kMachineSingleSelectControlEdgeInset *2;
        }
            break;
            case SocketMsgType_serverError:
        {
            cellHeight = 0.001;
        }
        default:
            break;
    }
    
//    NSLog(@"行高是：%.2f", cellHeight);
    
    return cellHeight;
}


@end
